Page({
  data: {
    sortOptions: ['按提交时间倒序排列', '按提交时间正序排列'],
    statusOptions: ['全部','已通过', '已提交', '已开始', '已结束', '已驳回' ],
    sortIndex: 0,
    statusIndex: 0,
    currentPage: 1,
    totalPages: 1,
    pages: 4,
    pageSize: 10,
    proposals: [],
    pageNum: 1,
    toPage: '',
    activities: [

    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   
  },
  loadPage() {
    const app = getApp()
    const status = this.data.statusOptions[this.data.statusIndex];
    const orderDirection = this.data.sortOptions[this.data.sortIndex];
    const pageNum = this.data.pageNum;
    app.activityApi.myActivity({status,orderDirection,pageNum}).then(
      res => {
        console.log(res)
        this.setData({
          activities: res.data.data.list,
          pageNum:res.data.data.pageNum,
          pages:res.data.data.pages
        })
      }
    )
  },
  handleSortChange(e) {
    this.setData({
      sortIndex: e.detail.value,
      pageNum:1
    })
    this.loadPage()
  },

  handleStatusChange(e) {
    this.setData({
      statusIndex: e.detail.value,
      pageNum:1
    })
    this.loadPage()
  },

  onCreateActivity() {
    wx.navigateTo({
      url: '/pages/activity/create'
    })
  },

  onPublishResult() {
    wx.navigateTo({
      url: '/pages/activity/submitResult'
    })
  },
  // 分页相关方法
  onPrevPage() {
    if (this.data.pageNum > 1) {
      this.setData({
        pageNum: this.data.pageNum - 1
      });
      this.loadPage();
    }
  },

  onNextPage() {
    if (this.data.pageNum < this.data.pages) {
      this.setData({
        pageNum: this.data.pageNum + 1
      });
      this.loadPage();
    }
  },

  onPageInput(e) {
    const page = parseInt(e.detail.value);
    if (page >= 1 && page <= this.data.pages) {
      this.setData({
        toPage: page
      });
    }
  },
  onPageSearch() {
    this.setData({
      pageNum:this.data.toPage
    })
    this.loadPage();
  },

  onJoinAddition(event) {
    const aid = event.currentTarget.dataset.aid
    wx.navigateTo({
      url: '/pages/activity/activityDetail?aid='+aid,
    })
  },

  onShow(){
    this.loadPage()
  },
  onPullDownRefresh() {
    this.loadPage()
    setTimeout(() => {
      wx.stopPullDownRefresh()
      wx.showToast({
        title: '刷新成功',
        icon: 'success',
        duration: 1000
      })
    }, 1000)
  },
  // 新增页面变化处理函数
  handlePageChange(e) {
    const { pageNum } = e.detail;
    this.setData({
      pageNum
    });
    this.loadPage();
  },

})